<div class="layuimini-main">
    <div class="layui-form layuimini-form" lay-filter="feedbackForm">
        <div class="layui-form-item">
            <label class="layui-form-label required">标题</label>
            <div class="layui-input-block">
                <input type="hidden" name="type" value="0">
                <input type="text" name="title" lay-verify="required" lay-reqtext="标题不能为空" placeholder="请输入邮件标题" value=""
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">邮件内容</label>
            <div class="layui-input-block">
                <textarea name="content" placeholder="请输入内容" class="layui-textarea"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">上传截图</label>
            <div class="layui-upload-drag" id="test10">
                <i class="layui-icon"></i>
                <p>点击上传，或将文件拖拽到此处</p>
                <div class="layui-hide" id="uploadDemoView">
                    <hr>
                    <img src="" alt="预览" style="max-width: 196px">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button id="save" class="layui-btn layui-btn-normal layui-hide">上传图片</button>
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form', 'axios', 'laydate', 'upload'], function () {
        var form = layui.form,
            layer = layui.layer,
            axios = layui.axios,
            upload = layui.upload,
            $ = layui.$;

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;

        //保存反馈信息
        function saveEmail(data) {
            // 发起异步请求
            axios({
                method: "post",
                url: 'email/feedbackEmail',
                data: data
            }).then(response => {
                //打印执行情况
                if (response.code == 0){
                    layer.msg("感谢您的反馈，管理员将针对您的问题进行解决", function () {
                        //关闭弹出层
                        window.location = './index.html';
                    })
                }else{
                    layer.msg(response.msg);
                }

            }).catch(error => {
                layer.msg(error);
            })
        }

        //拖拽上传
        upload.render({
            elem: '#test10'
            ,url: 'http://127.0.0.1:8080/email/feedbackImg' //改成您自己的上传接口
            ,size: 10000
            ,auto: false
            ,bindAction: '#save'
            ,headers: {
                token: localStorage.getItem("token")
            }
            ,choose: function (obj) {
                obj.preview(function(index, file, result){
                    $('#uploadDemoView').removeClass('layui-hide').find('img').attr('src', result);
                });
            }
            ,done: function (res) {
                if (res.code == 0){
                    var formData = form.val("feedbackForm");
                    formData.img = res.data;
                    console.log(formData)
                    saveEmail(formData);
                }

            }
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $('#save').click();
            return false;
        });
    });
</script>